package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc.cloud.cmclink.doc.entity.TblisustampFilesDO;
import com.cmc.cloud.cmclink.doc.mapper.TblisustampFilesMapper;
import com.cmc.cloud.cmclink.doc.vo.*;
import com.cmc.cloud.cmclink.doc.vo.specialcargoapplyvo.ApplyAndVoyageInfoVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import java.util.*;

import com.cmc.cloud.cmclink.doc.service.TblisustampMainService;
import com.cmc.cloud.cmclink.doc.entity.TblisustampMainDO;
import com.cmc.cloud.cmclink.doc.mapper.TblisustampMainMapper;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.TblisustampMainErrorCodeConstants;


/**
 * 申请主信息 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class TblisustampMainServiceImpl implements TblisustampMainService {

    @Resource
    private TblisustampMainMapper tblisustampMainMapper;

    @Resource
    private TblisustampFilesMapper tblisustampFilesMapper;

    @Override
    @Lock4j(name = "tblisustampMainUnique", keys = {"#createReqVO.getAgentCode", "#createReqVO.getPortCode"})
    public void createTblisustampMain(TblisustampMainCreateReqVO createReqVO) {
        // 插入
        validateUnique(createReqVO, null);
        TblisustampMainDO tblisustampMain = BeanUtil.copyProperties(createReqVO, TblisustampMainDO.class);
        tblisustampMainMapper.insert(tblisustampMain);
    }

    @Override
    @Lock4j(name = "tblisustampMainUnique", keys = {"#updateReqVO.getAgentCode", "#updateReqVO.getPortCode"})
    public void updateTblisustampMain(TblisustampMainUpdateReqVO updateReqVO) {
        // 校验存在
        validateTblisustampMainExists(updateReqVO.getId());
        validateUnique(updateReqVO, updateReqVO.getId());
        // 更新
        TblisustampMainDO updateObj = BeanUtil.copyProperties(updateReqVO, TblisustampMainDO.class);
        tblisustampMainMapper.updateById(updateObj);
    }

    @Override
    public void deleteTblisustampMain(Long id) {
        // 校验存在
        validateTblisustampMainExists(id);
        List<TblisustampFilesDO> tblisustampFilesDos = tblisustampFilesMapper.selectList(new LambdaQueryWrapperX<TblisustampFilesDO>().eq(TblisustampFilesDO::getMainId, id));
        if (CollectionUtil.isNotEmpty(tblisustampFilesDos)) {
            throw ServiceExceptionUtil.exception(TblisustampMainErrorCodeConstants.TBLISUSTAMP_NOT_DELETE);
        }
        // 删除
        tblisustampMainMapper.deleteById(id);
    }

    private void validateUnique(TblisustampMainBaseVO vo, Long id) {
        LambdaQueryWrapperX<TblisustampMainDO> queryWrapperX = new LambdaQueryWrapperX<TblisustampMainDO>()
                .eqIfPresent(TblisustampMainDO::getAgentCode, vo.getAgentCode())
                .eqIfPresent(TblisustampMainDO::getPortCode, vo.getPortCode());
        if (id != null) {
            queryWrapperX.ne(TblisustampMainDO::getId, id);
        }
        if (tblisustampMainMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(TblisustampMainErrorCodeConstants.TBLISUSTAMP_EXISTED);
        }
    }

    private void validateTblisustampMainExists(Long id) {
        if (tblisustampMainMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(TblisustampMainErrorCodeConstants.TBLISUSTAMP_MAIN_NOT_EXISTS);
        }
    }


    @Override
    public TblisustampMainDO getTblisustampMain(Long id) {
        return tblisustampMainMapper.selectById(id);
    }

    @Override
    public List<TblisustampMainDO> getTblisustampMainList(Collection<Long> ids) {
        return tblisustampMainMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TblisustampMainDO> getTblisustampMainPage(TblisustampMainPageReqVO reqVO) {
        LambdaQueryWrapperX<TblisustampMainDO> queryWrapperX = new LambdaQueryWrapperX<TblisustampMainDO>()
                .likeIfPresent(TblisustampMainDO::getAgentCode, reqVO.getAgentCode())
                .likeIfPresent(TblisustampMainDO::getPortCode, reqVO.getPortCode());
        return tblisustampMainMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public PageResult<TblisustampMainFilesVO> getMainFilesPage(TblisustampMainPageReqVO pageVO) {
        IPage<TblisustampMainFilesVO> page = Page.of(pageVO.getPageNo(), pageVO.getPageSize());
        IPage<TblisustampMainFilesVO> pageResult = tblisustampMainMapper.getTblisustampMainFiles(page, pageVO);
        List<TblisustampMainFilesVO> records = pageResult.getRecords();
        return new PageResult<>(records, pageResult.getTotal());
    }
}
